package com.vlong.town.controller.admin.notice;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.notice.NoticeInfo;
import com.vlong.town.service.notice.NoticeInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 公告信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/notice")
public class NoticeInfoController extends BaseController {

    // Fields

    @Autowired
    private NoticeInfoService noticeInfoService;// 公告信息业务实现类

    /**
     * 进入公告信息列表页面
     * @return notice/list_notice
     */
    @RequestMapping("/list")
    public String gotoNoticeInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<NoticeInfo> noticeInfoList = this.noticeInfoService.queryList(title, content, desc);
        PageInfo<NoticeInfo> pageInfo = new PageInfo<NoticeInfo>(noticeInfoList);
        PageTag<NoticeInfo> pt = new PageTag<NoticeInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        for(NoticeInfo noticeInfo : noticeInfoList) {
            noticeInfo.setContent(DataUtil.delHTMLTag(noticeInfo.getContent(), 0));
        }
        request.setAttribute("noticeInfoList", noticeInfoList);
        return "admin/notice/list_notice";
    }

    /**
     * 进入公告信息添加页面
     * @return notice/add_notice
     */
    @GetMapping("/addNotice")
    public String gotoNoticeInofAddPage() {
        return "admin/notice/add_notice";
    }

    /**
     * 进入公告信息修改页面
     * @return notice/edit_notice
     */
    @GetMapping("/editNotice/{noticeId}")
    public String gotoNoticeInofEditPage(@PathVariable(required=true, value="noticeId") Long noticeId) {
        NoticeInfo noticeInfo = this.noticeInfoService.queryById(noticeId);
        request.setAttribute("noticeInfo", noticeInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + noticeInfo.getThumbnail());
        return "admin/notice/edit_notice";
    }

    /**
     * 添加公告信息
     * @param title 公告标题
     * @param content 公告内容
     * @param desc 公告简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddNotice")
    public DataMessage addNotice(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("公告标题不合法");
            return dm;
        }
        this.noticeInfoService.add(1L, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("公告添加成功");
        return dm;
    }

    /**
     * 修改公告信息
     * @param title 公告标题
     * @param content 公告内容
     * @param desc 公告简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditNotice")
    public DataMessage editNotice(
            @RequestParam(name="noticeId", required=true) Long noticeId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("公告标题不合法");
            return dm;
        }
        this.noticeInfoService.update(noticeId, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("公告修改成功");
        return dm;
    }

    /**
     * 删除公告
     * @param noticeId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="noticeId") Long noticeId) {
        DataMessage dm = new DataMessage();
        if(noticeId != null && noticeId > 0L) {
            this.noticeInfoService.delete(noticeId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("公告删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除公告
     * @param noticeIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="noticeIds") String noticeIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(noticeIds, 1, 255)) {
            String[] noticeIdArray = noticeIds.split(",");
            for(String noticeId : noticeIdArray) {
                this.noticeInfoService.delete(Long.parseLong(noticeId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("公告删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

}
